﻿#include "GUICSVParameterDialog.h"
#include "ui_GUICSVParameterDialog.h"
#include "GUIAbsPostCurvePlotWidget.h"

#include "FITK_Component/FITKPlotWindow/FITKXYPlotProperty.h"
#include "FITK_Component/FITKPlotWindow/FITKXYCurveDrawManager.h"

#include "FITK_Component/FITKAcousticsPostData/FITKAcousticsCSVParameter.h"
#include "FITK_Component/FITKAcousticsPostData/FITKAcousticsPostData.h"

#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include <QTableWidget>
#include <QTableView>
#include <QCheckBox>
#include <QtCore/qmath.h>

namespace GUI
{
    GUICSVParameterDialog::GUICSVParameterDialog(Core::FITKActionOperator* oper, QWidget *parent, bool isAotoDelete) :
        GUIDialogBase(parent),
        _oper(oper),
        _ui(new Ui::GUICSVParameterDialog)
    {
        _ui->setupUi(this);
        // 去掉帮助按钮
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
        setWindowTitle(tr("PlotCurve"));
        //插入曲线窗口
        _curvePlotDialog = new GUIAbsPostCurvePlotWidget(_oper, this, false);
        _ui->verticalLayout_curvePlot->addWidget(_curvePlotDialog);
        _curvePlotDialog->setDBDefault(10);
        _curvePlotDialog->show();
    }

    GUICSVParameterDialog::~GUICSVParameterDialog()
    {
        if (_ui) delete _ui;
    }

    void GUICSVParameterDialog::setCSVPara(int objID, int parameterType)
    {
        _objID = objID;
        _parameterType = parameterType;
    }

    void GUICSVParameterDialog::init()
    {
        //获取后处理数据
        Acoustics::FITKAcousticsPostData* postData = FITKAPP->getGlobalData()->getPostData<Acoustics::FITKAcousticsPostData>();
        if (!postData) return;
        Acoustics::FITKAcousticsCSVParameterManager* csvParaManager = postData->getCSVParameterManager();
        if (!csvParaManager) return;
        Acoustics::FITKAcousticsCSVParameter* csvPara = csvParaManager->getCSVParameterData(_objID, (Acoustics::FITKAcousticsCSVParameter::CSVParameterType)_parameterType);
        if (!csvPara) return;
        //获取数据
        QString titleCurve;
        if (_parameterType == Acoustics::FITKAcousticsCSVParameter::CSVParameterType::ModalDensity)
            titleCurve = "Modal Density";
        else if (_parameterType == Acoustics::FITKAcousticsCSVParameter::CSVParameterType::LossFactor)
            titleCurve = "Loss Factor";
        else if (_parameterType == Acoustics::FITKAcousticsCSVParameter::CSVParameterType::CLF)
            titleCurve = "Coupling Loss Factor";
        else if (_parameterType == Acoustics::FITKAcousticsCSVParameter::CSVParameterType::RadiationEfficiency)
            titleCurve = "Radiation Efficiency";
        titleCurve = QString("%1 - %2").arg(titleCurve).arg(csvPara->getObjName());
        QHash<QString, std::vector<float>> paraValues = csvPara->getParameterValues();
        std::vector<float> frequencyValue = csvParaManager->getFrequency();
        int freqCount = frequencyValue.size();
        //初始化图表窗口数据
        Plot::FITKXYPlotProperty* xyPlotProp = _curvePlotDialog->getCurvePlotProp();
        if (!xyPlotProp) return;
        xyPlotProp->setTitle(titleCurve);
        xyPlotProp->setAxisName(tr("Frequency(Hz)"), " ");
        xyPlotProp->setAxisScaleEngine(0);
        xyPlotProp->setAxisScaleEngine(2);
        //初始化窗口
        _curvePlotDialog->initTableAndCurvePlot(&frequencyValue, &paraValues);
    }
}

